package cn.nawang.ebeim.api.controller;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.nawang.ebeim.api.domain.CustomerEncryption;
import cn.nawang.ebeim.api.service.CustomerEncryptionService;
import cn.nawang.ebeim.api.utils.AesPassword;
import cn.nawang.ebeim.api.utils.Constant;
import cn.nawang.ebeim.api.utils.ValidateUtil;
import cn.nawang.ebeim.key.NaWangKey;

/**
 * @ClassName: CustomerEncryptionController
 * @Description: TODO
 * @author fanyb
 * @date Oct 24, 2015 9:09:42 AM
 * @version v1.0
 */
@RestController
public class CustomerEncryptionController {
  private static final Logger LOG = LoggerFactory.getLogger(CustomerEncryptionController.class);

  @Autowired
  private CustomerEncryptionService customerEncryptionService;

  /**
   * 
   * @author fanyb
   * @Description: 查找客户加密对象
   * @param customerId
   * @return Map<String,Object>
   * @date Oct 24, 20159:12:03 AM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/token/customer_encryption/customer_id", method = RequestMethod.GET)
  public Map<String, Object> findUserStorageCloudList(String customerId) {
    LOG.info("/token/customer_encryption/customer_id" + customerId);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("code", Constant.CODE_ERROR);
    // 对encryptionKey解密
    CustomerEncryption customerEncryption = customerEncryptionService.findByCustomerId(customerId);
    if (!ValidateUtil.isNull(customerEncryption)) {
      String encryptionKey = customerEncryption.getEncryptionKey();
      try {
        encryptionKey = NaWangKey.decryptKey2(encryptionKey, AesPassword.getAesPassword());
        customerEncryption.setEncryptionKey(encryptionKey);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    map.put("obj", customerEncryption);
    map.put("code", Constant.CODE_SUCCESS);
    LOG.debug("findUserStorageCloudList {}", map);
    return map;
  }
}
